$(function () {

        var map = new BMap.Map("container");
        var point = new BMap.Point(116.3964, 39.9093);
        map.centerAndZoom(point, 13);
        map.enableScrollWheelZoom();
        map.addEventListener('click', function (e) {
            console.log(e);
            //通过标签的id值直接赋值给页面input
            longitude.value = e.point.lng;
            latitude.value = e.point.lat;
            //将获取的地址写到详情
            getAddress(longitude.value, latitude.value);
        });

        //当前初始的地理位置
        var geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function (r) {
            if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                var mk = new BMap.Marker(r.point);
                map.addOverlay(mk);
                map.panTo(r.point);
                console.log('您的位置：' + r.point.lng + ',' + r.point.lat);
            } else {
                alert('failed' + this.getStatus());
            }
        });

        //建立一个自动完成的对象
        var ac = new BMap.Autocomplete({
            "input": "suggestId",
            "location": map
        });

        ac.addEventListener("onhighlight", function (e) {//鼠标放在下拉列表上的事件
            var _value = "";
            var value = "";
            if (e.fromitem.index > -1) {
                _value = e.fromitem.value;
                value = _value.province + _value.city + _value.district + _value.street + _value.business;
            }

            value = "";
            if (e.toitem.index > -1) {
                _value = e.toitem.value;
                value = _value.province + _value.city + _value.district + _value.street + _value.business;
            }
        });

        var address;
        var pointArr = [];
        var pt = null;
        ac.addEventListener("onconfirm", function (e) {//鼠标点击确认事件
            var _value = e.item.value;
            address = _value.province + _value.city + _value.district + _value.street + _value.business;
            getlnglat(address);
        });

        function getlnglat(address) {
            $.ajax({
                type: 'POST',
                url: '/Admin/Map/getLnglatByAddress',
                data: {
                    address: address
                },
                dataType: 'json',
                async: false,
                success: function (data, status) {
                    lng = data.lng;
                    lat = data.lat;
                    $('#longitude').val(lng);
                    $('#latitude').val(lat);
                    getMap(lng, lat);
                },
                fail: function (err, status) {
                    console.log(err)
                }
            })
        }

        function getMap(lng, lat) {
            //定位到当前位置
            var geolocation = new BMap.Geolocation();
            geolocation.getCurrentPosition(function (r) {
                if (lng && lat) {
                    pt = new BMap.Point(lng, lat);
                    pointArr.push(pt);
                    var convertor = new BMap.Convertor();
                    convertor.translate(pointArr, 1, 5, translateCallback);
                }
            });
        }

        translateCallback = function (data) {
            if (data.status === 0) {
                console.log(data.points[data.points.length - 1]);
                var map = new BMap.Map("container");
                var point = new BMap.Point(data.points[data.points.length - 1].lng, data.points[data.points.length - 1].lat);
                map.centerAndZoom(point, 1);
                var marker = new BMap.Marker(data.points[data.points.length - 1]);//创建小红点
                map.addOverlay(marker);//将小红点放到地图上
                var label = new BMap.Label(address, {offset: new BMap.Size(20, -10)});//label
                //设置lebel样式
                label.setStyle({
                    width: "120px",
                    color: '#fff',
                    background: '#ff8355',
                    border: '1px solid "#ff8355"',
                    borderRadius: "5px",
                    textAlign: "center",
                    height: "26px",
                    lineHeight: "26px"
                });
                marker.setLabel(label); //添加百度label
                map.centerAndZoom(data.points[data.points.length - 1], 16);

                //设置精度圈
                var circle = new BMap.Circle(data.points[data.points.length - 1], 100); //创建圆
                //设置圆形的边线颜色
                circle.setStrokeColor("#00CCFF");
                //设置圆形的填充颜色
                circle.setFillColor("#00CCFF");
                //设置圆形的填充透明度，取值范围0 - 1
                circle.setFillOpacity(0.1);
                //设置圆形的边线透明度，取值范围0 - 1
                circle.setStrokeOpacity(0.01);
                //设置圆形边线的宽度，取值为大于等于1的整数
                circle.setStrokeWeight(1);
                map.addOverlay(circle); //增加圆
            }
        }

        function getAddress(lng, lat) {
            //console.log(lng)
            $.ajax({
                type: 'POST',
                url: '/Admin/Map/getAddressByLnglat',
                data: {
                    lng: lng,
                    lat: lat
                },
                dataType: 'json',
                success: function (data, status) {
                    if (data) {
                        $('#suggestId').val(data.address)
                    }
                    console.log(data);
                },
                fail: function (err, status) {
                    console.log(err)
                }
            })
        }
    }
)